package com.zy.crm.workbench.controller;


import com.zy.crm.vo.Message;
import com.zy.crm.vo.PaginationVo;
import com.zy.crm.workbench.domain.Water;
import com.zy.crm.workbench.service.WaterService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * @author CYH
 * 2021年11月13日测试功能完成
 */
//
@Controller
public class WaterController {

    @Resource
    private WaterService WaterService;


    // 添加用户信息，无误
    @RequestMapping("/workbench/water/saveWater.do")
    @ResponseBody
    public Object saveActivity(Water water){
        //测试
        System.out.println("进入保存成功，前端无误，继续排查");
        System.out.println(water);
        // 得到保存结果
        Message msg = WaterService.saveWater(water);

        return msg;
    }

    @RequestMapping("/workbench/water/pageList.do")
    @ResponseBody
    // 查询用分页
    public Object getPageList(String pageNo, String pageSize,Water water){
        //System.out.println("进入了查询界面");

       // System.out.println("测试数据传输了什么"+water);
        PaginationVo<Water> paginationVo = WaterService.getPageList(pageNo,pageSize,water);
        System.out.println(paginationVo);
        return paginationVo;
    }

    @RequestMapping("/workbench/water/pageList1.do")//报表打印
    @ResponseBody
    // 打印数据
    public Message getPageList1(Water water) throws IOException {

        List<Water> list = WaterService.getPrint(water);
       //System.out.println("获取的数据是"+paginationVo);
         Message msg=new Message();

        String path="D:\\ssm项目\\crm-master -day11\\";
        Workbook workbook=new HSSFWorkbook();//工作簿
        Sheet sheet =workbook.createSheet("航道水运表");//工作表
        //（1，1）
        Row row1=sheet.createRow(0);//第一行数据
        Cell cell11=row1.createCell(0);//第一列数据
        cell11.setCellValue("编号");//内容
        //(1,2)
        Cell cell12=row1.createCell(1);//第二列数据
        cell12.setCellValue("船名");//内容

        Cell cell13=row1.createCell(2);//第三列数据
        cell13.setCellValue("需缴费");//内容
        Cell cell14=row1.createCell(3);//第四列数据
        cell14.setCellValue("月份");//内容
        Cell cell15=row1.createCell(4);//第五列数据
        cell15.setCellValue("修改时间");//内容
        Cell cell16=row1.createCell(5);//第六列数据
        cell16.setCellValue("缴费状态");//内容
        String arr[][] = new String[list.size()][6];

        for(int i=0;i<list.size();++i){
            arr[i][0]=list.get(i).getId();
            arr[i][1]=list.get(i).getName();
            arr[i][2]=list.get(i).getCost();
            arr[i][3]=list.get(i).getMonth();
            arr[i][4]=list.get(i).getWrite_time();
            arr[i][5]=list.get(i).getMonth_status();
             }


        for(int i=1;i<=list.size();++i){
            Row row=sheet.createRow(i);//第I+1行数据
            for(int j=0;j<6;++j){
                Cell cell=row.createCell(j);
                cell.setCellValue(arr[i-1][j]);//
            }

        }


        //生成一个表
        FileOutputStream fileOutputStream= new FileOutputStream(path+"水运表.xls");

        workbook.write(fileOutputStream);
        //关闭流
        workbook.close();

        System.out.println("成功生成");


        msg.setSuccess(true);

         return  msg;
    }

    @RequestMapping("/workbench/water/delete.do")
    @ResponseBody
    public Message deleteActivity(HttpServletRequest request) {

        // 拿到ActivityId数组
        String WaterIds[] = request.getParameterValues("id");
        System.out.println("第一条数据是："+WaterIds);
        // 根据id数组删除市场活动cce5c8636660495d839bd0f661cc5d66
                           //cce5c8636660495d839bd0f661cc5d665
        Message msg = WaterService.deleteWater(WaterIds);
        System.out.println("删除成功");
        return msg;
    }


    // 修改市用户数据依据id,测试中
    @RequestMapping("/workbench/water/updateWater.do")
    @ResponseBody
    public Message updateActivityById(Water water){
       // System.out.println("成功添加数据进入了修改数据");
        System.out.println("测试传入的数据是否异常"+water);
        //测试到传入的name和id都是null，估计是前端问题
        Message msg = WaterService.updateWaterById(water);
        System.out.println("成功出来");
        return msg;

    }

    //根据用户id查询一条用户信息
    @RequestMapping("/workbench/water/getWaterById.do")
    @ResponseBody
    public Map<String , Object> getWaterById(String id){
        Map<String , Object> map = new HashMap<>();
        System.out.println("测试获取原来的信息"+id);
        // 查询到市场活动信息
        Water water = WaterService.getWaterById(id);
        //测试是否有结果返回,结果很明显是进入业务层出错，继续测试
        System.out.println("测试是否有结果返回");
        // 封装入map
        System.out.println(water);
        map.put("waterById",water);//测试结果是user里面有name的，是前端有问题

        return  map;
    }

}


















